import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    currentDate: new Date().getTime(),
    appointment: '', // 预约日期，2022-10-10
    description: '没有电了', // 问题描述
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    mobile: '13456781234',
    houseList: [
      // { name: '北京西三旗花园1号楼 101' },
      // { name: '北京东村家园3号楼 302' },
      // { name: '北京育新花园3号楼 703' },
      // { name: '北京天通苑北苑8号楼 403' },
    ],
    selectedHouse: {},
    repairItem: [
      // { name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }
    ],
    selectedRepair: {},
    attachment: [
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
  },
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    description: [
      { required: true, message: '请填写短信验证码!' }
    ]
  },
  async onLoad() {
    // 1. 发请求，取回房屋的数据
    const houseList = await wx.$http.get('/house')
    this.setData({ houseList })

    // 2. 发请求，取回维修数据
    const repairItem = await wx.$http.get('/repairItem')
    this.setData({ repairItem })
  },
  // 显示弹层
  selectHouse(e) {
    this.setData({ selectedHouse: e.detail })
  },
  openHouseLayer() { this.setData({ houseLayerVisible: true }) },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },

  // 报修信息
  selectRepair(e) { this.setData({ selectedRepair: e.detail }) },
  openRepairLayer() { this.setData({ repairLayerVisible: true }) },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  selectDate(e) {
    console.log(e)
    // 保存时间
    this.setData({
      dateLayerVisible: false,
      currentDate: e.detail,
      appointment: wx.$formatDate(e.detail)
    })
  },
  openDateLayer() { this.setData({ dateLayerVisible: true }) },
  closeDateLayer() { this.setData({ dateLayerVisible: false }) },

  afterRead(e) {
    const filePath = e.detail.file.tempFilePath
    console.log(filePath)
    // 上传这个文件

    wx.uploadFile({
      url: wx.$http.baseURL + '/upload',  // 接口文档中约定的文件上传的地址
      filePath,								// 本地文件的位置地址（临时的http打头的地址）
      name: 'file', 											// wx.uploadFile 要求必传。
      header: { 													// 接口文档中约定。 上传头像要携带token
        Authorization: 'Bearer ' + getApp().getToken() // 用户登录状态
      },
      // https://apifox.com/apidoc/shared-8d66c345-7a9a-4844-9a5a-1201852f6faa/api-42672275
      success: (result) => {
        const res = JSON.parse(result.data)
        console.log(res.data.url) 							// 上传成功的回调
        // this.setData({ [type]: res.data.url })
        // 追加到attachment
        const { attachment } = this.data
        attachment.push({ url: res.data.url })

        this.setData({ attachment })
      }
    })

  },
  delFile(e) {
    console.log(e)
    const idx = e.detail.index
    const { attachment } = this.data
    attachment.splice(idx, 1)
    this.setData({ attachment })
  },
  async goList() {
    // 表单校验
    if (!this.validate()) {
      return
    }
    const houseId = this.data.selectedHouse.id
    if (!houseId) {
      return wx.$toast('房屋不能为空')
    }
    const repairItemId = this.data.selectedRepair.id
    if (!repairItemId) {
      return wx.$toast('维修项目不能为空')
    }
    const { mobile, appointment, description, attachment } = this.data
    // 调用接口提交数据
    await wx.$http.post('/repair', {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    // 页面跳转
    // wx.reLaunch({
    //   url: '/repair_pkg/pages/list/index',
    // })
    wx.navigateBack({
      delta: 1
    })
  },
})
